using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.Web;
using ECommerce.DAL;
using Entity;
using log4net;
using ECommerce.Library;

namespace AutoGear.BLL
{
    public class AutoGearSiteUser : SiteUser
    {
        //protected int _userId;
        public int UserId
        {
            get { return base._userId; }
        }

        protected bool _isActive;
        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }

        protected bool _isDeleted;
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }

        public static bool IsUserExists(string userName)
        {
            try
            {
                CustomCollection<TblUser> tblUser = TblUserExtended.GetUserByUserName(userName.Trim());
                if (tblUser.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {

                // Setting log information about the exception
                StringBuilder logText = new StringBuilder("Duplicate user finding caused server error: Parameters( ");
                logText = logText.Append("userName -> ").Append(userName);
                logText = logText.Append(" )");

                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AutoGearSiteUser");
                if (log.IsErrorEnabled)
                    log.Error(logText.ToString(), ex);
                throw ex;
            }
        }
        public static bool IsLoginValid(string userName, string password)
        {
            try
            {
                CustomCollection<TblUser> tblUser = TblUserExtended.GetUserByUserName(userName);
                if (tblUser.Count > 0 && tblUser[0].Password == password && tblUser[0].IsActive && !tblUser[0].IsDeleted)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {

                // Setting log information about the exception
                StringBuilder logText = new StringBuilder("IsLoginValid caused server error: Parameters( ");
                logText = logText.Append("userName -> ").Append(userName);
                logText = logText.Append("password -> ").Append(password);
                logText = logText.Append(" )");

                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AutoGearSiteUser");
                if (log.IsErrorEnabled)
                    log.Error(logText.ToString(), ex);
                throw ex;
            }
        }
        public static UserRoleType GetUserRoleByUserId(int userId)
        {
            CustomCollection<TblUserRole> tblUserRole = TblUserRole.GetTblUserRoleByUserId(userId);
            string strUserRoleName = tblUserRole[0].RoleName;
            if (strUserRoleName == UserRoleType.AdminUser.ToString())
            {
                return UserRoleType.AdminUser;
            }
            else if (strUserRoleName == UserRoleType.CustomerAdmin.ToString())
            {
                return UserRoleType.CustomerAdmin;
            }
            else if (strUserRoleName == UserRoleType.CustomerUser.ToString())
            {
                return UserRoleType.CustomerUser;
            }
            else
            {
                return UserRoleType.SuperAdminUser;
            }
        }
        public static bool Login(string userName, string password, UserLocationType userLocation)
        {
            try
            {
                if (IsLoginValid(userName, password))
                {
                    SiteUser siteUser = UserFactory.BuildSiteUser(userName, password);
                    AutoGearSiteUser autoGearSiteUser = siteUser as AutoGearSiteUser;

                    if ((siteUser is AdminUser || siteUser is SuperAdminUser || siteUser is CustomerAdmin) && userLocation == UserLocationType.BackEnd)
                    {
                        SessionManager.LoggedAdminUser = siteUser;
                    }
                    if ((siteUser is CustomerAdmin || siteUser is CustomerUser) && userLocation == UserLocationType.FrontEnd)
                    {
                        SessionManager.LoggedUser = siteUser;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                // Setting log information about the exception
                StringBuilder logText = new StringBuilder("Login caused server error: Parameters( ");
                logText = logText.Append("userName -> ").Append(userName);
                logText = logText.Append("password -> ").Append(password);
                logText = logText.Append(" )");

                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AutoGearSiteUser");
                if (log.IsErrorEnabled)
                    log.Error(logText.ToString(), ex);
                throw ex;
            }
        }
        public static bool Logout(UserLocationType userLocation)
        {
            try
            {

                if (userLocation == UserLocationType.FrontEnd)
                {
                    SessionManager.LoggedUser = null;
                }
                else
                {
                    SessionManager.LoggedAdminUser = null;
                }
                if (CookieManager.IsCookieExists(CookieManager.USER_NAME))
                {
                    CookieManager.RemoveCookie(CookieManager.USER_NAME);
                    CookieManager.RemoveCookie(CookieManager.PASSWORD);
                }
                return true;
            }
            catch (Exception ex)
            {

                // Setting log information about the exception
                StringBuilder logText = new StringBuilder("Logout caused server error: Parameters( ");
                logText = logText.Append("userLocation -> ").Append(userLocation);
                logText = logText.Append(" )");

                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AutoGearSiteUser");
                if (log.IsErrorEnabled)
                    log.Error(logText.ToString(), ex);
                throw ex;
            }

        }
        public bool UpdatePassword(string password)
        {            
            TblUser updatedTblUser = TblUser.GetTblUserByUserId(_userId);
            if (updatedTblUser != null)
            {
                updatedTblUser.Password = password;
                return updatedTblUser.Save();
            }
            return false;
        }
    }
}
